package models.dao;

import java.util.ArrayList;
import java.util.List;

import play.Logger;

import models.User;

public class UserDAO {

	public static User getUserById(long id) {
		User ret = null;
		try {
			ret = User.findById(id);
		} catch (Exception ex) {

		}
		return ret;
	}

	public static User getOrCreateUser(String userName, String ip) {
		User ret = null;
		try {
			List<User> users = User.find("userName = ?", userName).fetch();

			if (users.size() > 0) {
				ret = users.get(0);
			} else {
				ret = new User();
				ret.userName = userName;
				ret.ip = ip;
				ret.save();
			}

		} catch (Exception ex) {
			ret = null;
		}
		return ret;

	}

	public static List<User> getRegisteredUser() {
		List<User> ret = new ArrayList<User>();
		try {
			List<User> users = User.find("registered = ?", 1).fetch();
			for (User u : users) {
				if (u.isUserNameAEmail()) {
					ret.add(u);
				}
			}

		} catch (Exception ex) {
			Logger.error(ex, "", "");
		}
		return ret;
	}

}
